package com.whit.nuoge.servlet;

import com.whit.nuoge.pojo.Page;
import com.whit.nuoge.pojo.Usergroup;
import com.whit.nuoge.service.UsergroupService;
import com.whit.nuoge.service.impl.UsergroupServiceImpl;
import com.whit.nuoge.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@WebServlet(value = "/manager/usergroupServlet")
public class UsergroupServlet extends  BaseServlet{

    private UsergroupService usergroupService = new UsergroupServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
        Integer pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String queryString = req.getParameter("queryString");

        Page<Usergroup> page = usergroupService.page(pageNo,pageSize,queryString);

        StringBuilder sb = new StringBuilder("manager/usergroupServlet?action=page");
        if (req.getParameter("queryString") != null) {
            sb.append("&queryString=").append(req.getParameter("queryString"));
        }

        page.setUrl(sb.toString());

        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/usergroup/usergroup_manager.jsp").forward(req,resp);
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Usergroup> usergroups = usergroupService.query();
        request.setAttribute("usergroups",usergroups);
        request.getRequestDispatcher("/pages/usergroup/usergroup_manager.jsp").forward(request,response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Usergroup usergroup = WebUtils.copyParamToBean(new Usergroup(),request.getParameterMap());
        usergroupService.save(usergroup);
        response.sendRedirect(request.getContextPath() + "/manager/usergroupServlet?action=page&pageNo="
                + Integer.MAX_VALUE);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = WebUtils.parseInt(request.getParameter("id"),0);
        String ids = request.getParameter("ids");
        String[] split = ids.split(",");
        for(String delId :split){
            usergroupService.deleteById(Integer.parseInt(delId));
        }

        String queryString = request.getParameter("queryString");
        if(queryString != null && queryString != ""){
            queryString =  URLEncoder.encode(queryString,"UTF-8");

        }
        response.sendRedirect(request.getContextPath()
                + "/manager/usergroupServlet?action=queryForPageItemsByQueryString&pageNo="
                + request.getParameter("pageNo")+ "&queryString=" + queryString);
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Usergroup usergroup = WebUtils.copyParamToBean(new Usergroup(),request.getParameterMap());


        usergroupService.update(usergroup);
        String queryString = request.getParameter("queryString");
        if(queryString != null && queryString != ""){
            queryString =  URLEncoder.encode(queryString,"UTF-8");

        }

        response.sendRedirect(request.getContextPath()
                + "/manager/usergroupServlet?action=queryForPageItemsByQueryString&pageNo="
                + request.getParameter("pageNo")+ "&queryString=" + queryString);
    }

    protected void getUsergroup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = WebUtils.parseInt(request.getParameter("id"),0);
        Usergroup usergroup = usergroupService.queryById(id);
        request.setAttribute("usergroup",usergroup);
        request.getRequestDispatcher("/pages/usergroup/usergroup_edit.jsp").forward(request,response);
    }

    protected void getUsergroupShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = WebUtils.parseInt(request.getParameter("id"),0);
        Usergroup usergroup = usergroupService.queryById(id);
        request.setAttribute("usergroup",usergroup);
        request.setAttribute("isShow","isShow");
        request.getRequestDispatcher("/pages/usergroup/usergroup_edit.jsp").forward(request,response);
    }

        protected void queryForPageItemsByQueryString(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
        Integer pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String queryString = req.getParameter("queryString");
        Page<Usergroup> page = usergroupService.pageByQueryString(pageNo,pageSize,queryString);

        StringBuilder sb = new StringBuilder("manager/usergroupServlet?action=queryForPageItemsByQueryString");
        if (req.getParameter("queryString") != null) {
            sb.append("&queryString=").append(req.getParameter("queryString"));
        }

        page.setUrl(sb.toString());

        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/usergroup/usergroup_manager.jsp").forward(req,resp);
    }
}
